package com.zx.servicelearn.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.zx.servicelearn.R;
import com.zx.servicelearn.service.MyService;


/**
 * @作者 zhoux
 * @创建时间 2017/12/21
 * @描述 普通service学习
 * @修改时间 2017/12/21
 * @修改描述
 * @修改者 zhoux
 **/
public class ServiceLearn1Activity extends AppCompatActivity {
    String TAG = "ServiceLearn1Activity";
    TextView testShow_tv;

    /**
     * 主线程handler，用于处理UI
     */
    Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    String msgText = (String) msg.obj;
                    if (msgText != null)
                        testShow_tv.setText("" + msgText);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_learn1);
        testShow_tv = findViewById(R.id.testShow_tv);
    }

    public void starServiceTest(View view) {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    public void stopServiceTest(View view) {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }

    private MyService.MyBinder myBinder;

    ServiceConnection myServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG, "myServiceConnection exc");//

            myBinder = (MyService.MyBinder) service;
            myBinder.testInService(mainHandler);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void bindServiceTest(View view) {
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, myServiceConnection, BIND_AUTO_CREATE);//第三个参数是一个标志位，这里传入BIND_AUTO_CREATE表示在Activity和Service建立关联后自动创建Service，这会使得MyService中的onCreate()方法得到执行，但onStartCommand()方法不会执行。
    }

    public void unBindServiceTest(View view) {
        try {
            unbindService(myServiceConnection);
        } catch (Exception e) {

        }
    }

    public void gotoAct2(View view) {
        Intent intent = new Intent(this, ServiceLearn2Activity.class);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            unbindService(myServiceConnection);//必须在onDestory中调用unbindService，否则会出现异常。Activity xxx has leaked ServiceConnection xxx that was originally bound here
        } catch (Exception e) {

        }
    }
}
